package com.haier.controller.h5;

import java.util.Date;
import java.util.List;

import com.haier.vo.ApplyListVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.haier.constant.HpConstant;
import com.haier.dto.HpGoApplyDto;
import com.haier.service.IHpGoApplyService;
import com.haier.utils.EmptyUtils;
import com.haier.utils.GetUtils;
import com.haier.vo.ResponseVo;

@RestController
@RequestMapping(value = "/hipassjava/h5/goApply")
public class H5GoApplyContoller {
	private static final Logger logger = LoggerFactory.getLogger(H5GoApplyContoller.class);
	@Autowired
	private IHpGoApplyService iHpGoApplyService;

	/**
	 * 申请
	 */
	@RequestMapping(value = "/apply", method = RequestMethod.POST)
	public Object apply(@RequestBody JSONObject params) {
		List<ApplyListVo> applyListVo = JSONObject.parseArray(params.getJSONArray("applyListVo").toString(), ApplyListVo.class );
		if(EmptyUtils.isEmpty(applyListVo)){
			return ResponseVo.checkError("请选择!");
		}
		String date = GetUtils.getDateString(new Date(), "yyyy-MM-dd");
		String error = "";
		try {
			for (ApplyListVo vo : applyListVo) {
				if(EmptyUtils.isEmpty(vo.getJobNumber())){
					error = vo.getAreaName()+ "的工号不能为空";
					break;
				}
				if(EmptyUtils.isEmpty(vo.getName())){
					error = vo.getAreaName()+ "的名称不能为空";
					break;
				}
				if (HpConstant.IS_AUDIT_NO.equals(vo.getIsAudit())){
					if(EmptyUtils.isEmpty(vo.getAuditor())){
						error = vo.getAreaName()+ "的审批人不能为空";
						break;
					}
					if(EmptyUtils.isEmpty(vo.getAuditorName())){
						error = vo.getAreaName()+ "的审批人名称能为空";
						break;
					}
				}
				if(EmptyUtils.isEmpty(vo.getDateDay())){
					error = vo.getAreaName()+ "的日期不能为空";
					break;
				}
				if (date.compareTo(vo.getDateDay()) > 0) {
					error = vo.getAreaName()+ "不能申请过去的日期";
					break;
				}
				if(EmptyUtils.isEmpty(vo.getGoCount())){
					error = vo.getAreaName()+ "的通行次数不能为空";
					break;
				}
				if(EmptyUtils.isEmpty(vo.getApplyReason())){
					error = vo.getAreaName()+ "的申请次数不能为空";
					break;
				}
			}
			if (!EmptyUtils.isEmpty(error)) {
				return ResponseVo.checkError(error);
			}
			return iHpGoApplyService.apply(applyListVo);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 查询分页
	 */
	@RequestMapping(value = "/page", method = RequestMethod.POST)
	public Object page(@RequestBody HpGoApplyDto dto) {
		try {
			if (EmptyUtils.isEmpty(dto.getAuditor()) && EmptyUtils.isEmpty(dto.getJobNumber())) {
				return ResponseVo.checkError("审核人和申请人不能同时为空");
			}
			return ResponseVo.success(iHpGoApplyService.pageByH5(dto));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 获取详情
	 */
	@RequestMapping(value = "/getDetail", method = RequestMethod.POST)
	public Object getDetail(@RequestBody HpGoApplyDto dto) {
		try {
			return iHpGoApplyService.getDetail(dto);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 同意
	 */
	@RequestMapping(value = "/agree", method = RequestMethod.POST)
	public Object agree(@RequestBody HpGoApplyDto dto) {
		try {
			dto.setStatus(HpConstant.HP_GO_APPLY_STATUS_20);
			return iHpGoApplyService.setStatus(dto);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 驳回
	 */
	@RequestMapping(value = "/reject", method = RequestMethod.POST)
	public Object reject(@RequestBody HpGoApplyDto dto) {
		try {
			dto.setStatus(HpConstant.HP_GO_APPLY_STATUS_F10);
			return iHpGoApplyService.setStatus(dto);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}
}
